package com.wanpinghui.supplier.widgets;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.object.FeedBackActivity;
import com.wanpinghui.supplier.object.ObjectDetailActivity;
import com.wanpinghui.supplier.user.MinePackageActivity2;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBean;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBidSuccessExt;
import com.yunshl.yunshllibrary.utils.TextUtil;

/**
 * Created by tg596 on 2018/1/13.
 */

public class MsgLeftOrderNotificationView extends LinearLayout {


    private TextView tv_phone;
    private TextView tv_order_detail;
    private TextView tv_content;
    private TextView tv_type;
    private SpinView sv;
    private ImageView iv_error;
    private MsgBidSuccessExt msgBidSuccessExt;
    private TextView tv_msg;
    private LinearLayout tv_phone_ll;
    private View msgLine;

    public MsgLeftOrderNotificationView(Context context) {
        super(context);
        init(context);
    }

    public MsgLeftOrderNotificationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MsgLeftOrderNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MsgLeftOrderNotificationView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        View mView = LayoutInflater.from(context).inflate(R.layout.item_msg_left_order_notification, this);
        tv_order_detail = (TextView) mView.findViewById(R.id.tv_order_detail);
        tv_phone = (TextView) mView.findViewById(R.id.tv_phone);
        tv_msg = (TextView) mView.findViewById(R.id.tv_msg);
        tv_content = (TextView) mView.findViewById(R.id.tv_content);
        msgLine = mView.findViewById(R.id.message_line);
        tv_type = (TextView) mView.findViewById(R.id.tv_type);
        sv = (SpinView) mView.findViewById(R.id.sv);
        iv_error = (ImageView) mView.findViewById(R.id.iv_error);
        tv_phone_ll = (LinearLayout) mView.findViewById(R.id.tv_phone_ll);

    }

    public void initData(final MsgBean msgBean) {
        if (TextUtil.isNotEmpty(msgBean.getExt())) {
            try {
                msgBidSuccessExt = new Gson().fromJson(msgBean.getExt(), new TypeToken<MsgBidSuccessExt>() {
                }.getType());
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
        if (msgBidSuccessExt != null) {
            tv_msg.setText(msgBean.getMsg());
            tv_type.setText(msgBidSuccessExt.getDemand_type());
            if(TextUtil.isNotEmpty(msgBidSuccessExt.getDemand_info())){
                String messageStr = msgBidSuccessExt.getDemand_info().replaceAll("\n\n","\n").replaceAll("\n\n","\n");
                final String phone = msgBidSuccessExt.getMobile();
                if (TextUtil.isPhoneNumber(phone)) {
                    SpannableString spannableString = new SpannableString(messageStr);
                    spannableString.setSpan(new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getContext().startActivity(intent);
                        }

                        @Override
                        public void updateDrawState(TextPaint ds) {
                            ds.setUnderlineText(false);
                        }
                    }, messageStr.indexOf(phone), messageStr.indexOf(phone) + 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorPrimaryBlue))
                            , messageStr.indexOf(phone), messageStr.indexOf(phone) + 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv_content.setText(spannableString);
                    tv_content.setMovementMethod(LinkMovementMethod.getInstance());
                }else {
                    tv_content.setText(messageStr);
                }
            }
            if(msgBean.getType() == 5){
                tv_order_detail.setText("订单详情>");
            }else if (msgBean.getType() == 6){
                tv_type.setVisibility(View.GONE);
                tv_content.setVisibility(View.GONE);
                msgLine.setVisibility(View.GONE);
                tv_order_detail.setText("我的账户>");
            }else if(msgBean.getType() == 7){
                tv_order_detail.setText("提交申请>");
            }
            tv_order_detail.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (msgBean.getType()){
                        case 5:
                            Intent intent5 = new Intent(getContext(), ObjectDetailActivity.class);
                            intent5.putExtra("id", msgBidSuccessExt.getDemand_id());
                            intent5.putExtra("notification", true);
                            getContext().startActivity(intent5);
                            break;
                        case 6:
                            Intent intent6 = new Intent(getContext(), MinePackageActivity2.class);
                            getContext().startActivity(intent6);
                            break;
                        case 7:
                            Intent intent7 = new Intent(getContext(), FeedBackActivity.class);
                            intent7.putExtra("id", msgBidSuccessExt.getDemand_id());
                            try {
                                intent7.putExtra("success_at", Long.parseLong(msgBidSuccessExt.getBid_win_at()));
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            intent7.putExtra("notification", true);
                            getContext().startActivity(intent7);
                            break;
                    }

                }
            });

            switch (msgBean.getSend_statu()) {
                case 0:
                    setError(false);
                    setSending(true);
                    break;
                case 1:
                    setError(false);
                    setSending(false);
                    break;
                case 2:
                    setError(true);
                    setSending(false);
                    break;
            }
        }
    }

    public void setSending(boolean isSendding) {
        if (isSendding)
            sv.setVisibility(View.VISIBLE);
        else
            sv.setVisibility(View.GONE);

    }

    public void setError(boolean isError) {
        if (isError)
            iv_error.setVisibility(View.VISIBLE);
        else
            iv_error.setVisibility(View.GONE);
    }

}
